package com.kotlin.coroutine.to_use.domain

import com.kotlin.coroutine.mybatis.ContentMapper
import com.kotlin.coroutine.mybatis.GameMapper
import kotlinx.coroutines.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class NotSuspendService {
    @Autowired
    private lateinit var gameMapper: GameMapper
    @Autowired
    private lateinit var contentMapper: ContentMapper

    fun notSuspend(){
        // 若想要在类中不声明suspend，还想要返回值，则需要使用async
        val n1:Deferred<Int> = somethingUsefulOneAsync() // async的返回值是Deferred类型的，它是job类的子类，可以获取返回值
        val n2 = somethingUsefulOneAsync2()
        val n3 = somethingUsefulOneAsync3()
        val n4 = somethingUsefulOneAsync4()

        // 判断协程是否执行完成
        println(n1.isCompleted)
        // 拿到协程的返回结果。如果isCompleted为false，会报错
        print(n1.getCompleted())
        // await 为延迟获取到返回成功的结果，无需判断是否isCompleted，但需要协程的方法进行调用，如协程高阶函数、suspend修饰的方法。
        runBlocking {
            print(n1.await())
        }
        print(n4)

    }
    fun notSuspend2(){
        val n1:String = test1("test01")
        println("--------------")
        test2("test02")
        println("---------------")
        test3("test03")
    }

    /***-----------notSuspend - start ---------------*/
    fun somethingUsefulOneAsync(): Deferred<Int> = GlobalScope.async {
        delay(2000L)
        println("1")
        200
    }
    fun somethingUsefulOneAsync2() = GlobalScope.async {
        delay(2000L)
        println("2")
        return@async "200"
    }
    fun somethingUsefulOneAsync3() = GlobalScope.async {
        delay(2000L)
        println("3")
        return@async SearchContentDTO("","","","")
    }
    fun somethingUsefulOneAsync4() = GlobalScope.async {
        delay(2000L)
        println("4")
    }
    /***-----------notSuspend - end ---------------*/


    /***-----------notSuspend2 - start ---------------*/

    fun test1(word: String) = runBlocking {
        delay(2000)
        GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("1")
            delay(1000)

        }
        GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("2")
            delay(1000)
        }
        GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("3")
            delay(1000)
        }
        GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("4")
            delay(1000)
        }
        GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("5")
            delay(1000)
        }

        delay(2000)
        return@runBlocking "test1"
    }
    fun test2(word: String) = runBlocking {
        GlobalScope.async {
            withContext(Dispatchers.Unconfined) {  // 👈 切换到 IO 线程，并在执行完成后切回 UI 线程
                println("1")
                delay(1000)
            }
            withContext(Dispatchers.Unconfined) {  // 👈 切换到 IO 线程，并在执行完成后切回 UI 线程
                println("2")
                delay(1000)
            }
            withContext(Dispatchers.Unconfined) {  // 👈 切换到 IO 线程，并在执行完成后切回 UI 线程
                println("3")
                delay(1000)
            }
            withContext(Dispatchers.Unconfined) {  // 👈 切换到 IO 线程，并在执行完成后切回 UI 线程
                println("4")
                delay(1000)
            }
        }
        delay(2000)
    }
    fun test3(word: String) = runBlocking {
        val s: Deferred<String> = GlobalScope.async {
            delay(2000L)
            println("1")
            "200"
        }

        while(!s.isCompleted){

        }
        val completed:String = s.getCompleted()
        println("执行完成")
        println(completed)

        GlobalScope.async {
            delay(2000L)
            println("2")
        }
        GlobalScope.async {
            delay(2000L)
            println("3")
        }
        GlobalScope.async {
            delay(2000L)
            println("4")
        }
        GlobalScope.async {
            delay(2000L)
            println("5")
        }
    }
    /***-----------notSuspend2 - end ---------------*/

    fun test4(){
        val s: Job = GlobalScope.launch {
            // 在 runBlocking 作用域中启动一个新协程

            println("1")
            delay(1000)
            ""
        }


    }
}

